package org.dawn.deveco.plugin.resourceeditor.test;

import javax.swing.*;
import javax.swing.plaf.basic.BasicTableUI;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class TableRowHoverExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("JTable Row Hover Example");
        JTable table = new JTable(10, 5); // 创建一个10行5列的JTable

        // 设置鼠标悬停时行的高亮颜色
        table.setSelectionForeground(table.getForeground());
        table.setSelectionBackground(Color.BLUE);
        table.setUI(new CustomTableUI());

        frame.add(new JScrollPane(table));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    static class CustomTableUI extends BasicTableUI {
        private boolean mouseOverRow = false;

        private boolean needPaint = false;
        private int currentRow = -1;

        private int currentCol = -1;

        private Color backgroundColor = new Color(0xF0F0F0);

        @Override
        protected void installListeners() {
            super.installListeners();

/*
            table.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseEntered(MouseEvent e) {
                    mouseOverRow = true;
                    int row = table.rowAtPoint(e.getPoint());
                    int col = table.columnAtPoint(e.getPoint());
                    if (row < 0 || row >= table.getRowCount()
                        || col < 0 || col >= table.getColumnCount()) {
                        return;
                    }
                    Rectangle rowBounds = table.getCellRect(row, col, true);
//                    Rectangle rowBounds = table.getCellRect(0, 0, true);
                    table.repaint(rowBounds);
                    System.out.printf("== enter, (%d, %d)%n", row, col);
                }

                @Override
                public void mouseExited(MouseEvent e) {
                    mouseOverRow = false;
                    int row = table.rowAtPoint(e.getPoint());
                    int col = table.columnAtPoint(e.getPoint());
                    if (row < 0 || row >= table.getRowCount()
                            || col < 0 || col >= table.getColumnCount()) {
                        return;
                    }
                    Rectangle rowBounds = table.getCellRect(row, col, true);
//                    Rectangle rowBounds = table.getCellRect(table.getRowCount() - 1, 0, true);
                    table.repaint(rowBounds);
                    System.out.printf("== exit, (%d, %d)%n", row, col);
                }
            });
*/
            table.addMouseMotionListener(new MouseAdapter() {
                @Override
                public void mouseMoved(MouseEvent e) {
                    int row = table.rowAtPoint(e.getPoint());
                    int col = table.columnAtPoint(e.getPoint());
                    if (row < 0 || row >= table.getRowCount()
                            || col < 0 || col >= table.getColumnCount()) {
                        needPaint = false;
                        return;
                    }
                    needPaint = true;

                    if (row == currentRow && col == currentCol) {
                        return;
                    }

                    // 恢复默认背景色
                    backgroundColor = table.getBackground();
                    table.repaint(table.getCellRect(currentRow, currentCol, true));

                    currentRow = row;
                    currentCol = col;

                    backgroundColor = Color.BLUE;
                    Rectangle rowBounds = table.getCellRect(row, col, true);
                    table.repaint(rowBounds);
                    System.out.printf("== move, (%d, %d), %s%n", row, col, rowBounds);

//                    needPaint = false;
                }
            });
        }

        @Override
        public void paint(Graphics g, JComponent c) {
            super.paint(g, c);
//            if (mouseOverRow) {
//                int row = table.rowAtPoint(table.getMousePosition());
//                if (row >= 0 && row < table.getRowCount()) {
//                    Rectangle rowBounds = table.getCellRect(row, 0, true);
//                    g.setColor(table.getSelectionBackground());
//                    g.fillRect(rowBounds.x, rowBounds.y, table.getWidth(), rowBounds.height);
//                }
//            }

            if (needPaint) {
                int row = table.rowAtPoint(table.getMousePosition());
                int col = table.columnAtPoint(table.getMousePosition());
                if (row < 0 || row >= table.getRowCount()
                        || col < 0 || col >= table.getColumnCount()) {
                    return;
                }
                Rectangle rowBounds = table.getCellRect(row, col, true);
                g.setColor(backgroundColor);
                g.fillRect(rowBounds.x, rowBounds.y, rowBounds.width, rowBounds.height);
            }
        }
    }
}